package com.allen;

import com.allen.Service.BackServerHandler;
import com.allen.Service.CustomClient;
import com.allen.Service.CustomClientHandler;
import com.allen.Util.FileUtil;
import com.allen.pojo.Account;
import com.allen.pojo.Custom;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class Back {
    public static  final  int port =8088;
    public static ServerSocket serverSocket = null;
    private static HashMap<String,Account> account;
    //存款
    public static  synchronized void deposit(String accountid, BigDecimal cal){
        account.get(accountid).setBalance(cal.add(account.get(accountid).getBalance()));
    }
    //取款
    public static synchronized String withdraw(String accountid,BigDecimal cal){
        if(account.get(accountid).getBalance().subtract(cal).compareTo(BigDecimal.ZERO)==-1){
            return "金额不足";
        }
        account.get(accountid).setBalance(account.get(accountid).getBalance().subtract(cal));
        return "取款金额："+cal.toString()+"余额为："+account.get(accountid).getBalance().toString();
    }
    public static String query(String accountid){
        return account.get(accountid).getBalance().toString();
    }
    public static void main(String[] args) throws Exception {
        FileUtil fileUtil = new FileUtil();
//                List<Custom> customs = Arrays.asList(
//                new Custom("001","111111199700000000","胡洋洋",23,'1'),
//                new Custom("002","111111198200000000","范冰冰",40,'0')
//        );
//        fileUtil.Serialize(customs,Custom.class);
//        System.out.println("序列化成功");

        List<Account> accountList = fileUtil.Deserialize(Account.class);
        System.out.println(accountList);
        List<Custom> customList = fileUtil.Deserialize(Account.class);
        System.out.println(customList);
        ServerSocket serverSocket = null;
        try {
            serverSocket=new ServerSocket(9999);
            while (true){
                Socket socket = serverSocket.accept();  //阻塞
                System.out.println("客户端"+socket.getRemoteSocketAddress().toString()+"来连接了");
                ExecutorService executorService = Executors.newFixedThreadPool(10);
                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
                threadPoolExecutor.setCorePoolSize(10);

                executorService.execute(new Thread(new BackServerHandler(socket)));
                executorService.shutdown();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
